export default {
  //展开树形数据方法
  handleTree(data: any, id: string, parendId: string, children: string) {
    let config = {
      id: id || 'id',
      parendId: parendId || 'parentId',
      childrenList: children || 'children',
    }
    var childrenListMap = [] as any
    var nodeIds = []
    var tree = []
    for (let item of data) {
      let parendId = item[config.parendId]
      if (childrenListMap[parendId] == null) {
        childrenListMap[parendId] = []
      }
      nodeIds[item[config.id]] = item
      childrenListMap[parendId].push(item)
    }
    for (let item of data) {
      let parendId = item[config.parendId]
      if (nodeIds[parendId] == null) {
        tree.push(item)
      }
    }

    for (let t of tree) {
      adaptToChildrenList(t)
    }
    function adaptToChildrenList(o: any) {
      if (childrenListMap[o[config.id]] !== null) {
        o[config.childrenList] = childrenListMap[o[config.id]]
      }
      if (o[config.childrenList]) {
        for (let c of o[config.childrenList]) {
          adaptToChildrenList(c)
        }
      }
    }
    return tree
  },
}
